package com.kakarote.examine.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.google.common.collect.Lists;
import com.kakarote.common.log.entity.OperationLog;
import com.kakarote.common.log.enums.BehaviorEnum;
import com.kakarote.core.common.enums.FieldEnum;
import com.kakarote.core.common.enums.SystemCodeEnum;
import com.kakarote.core.entity.BasePage;
import com.kakarote.core.entity.QueryParentDeptBO;
import com.kakarote.core.exception.CrmException;
import com.kakarote.core.feign.admin.entity.ExamineEmailBO;
import com.kakarote.core.feign.admin.entity.SimpleUser;
import com.kakarote.core.feign.admin.service.AdminService;
import com.kakarote.core.feign.examine.entity.ExamineFlowFinalBO;
import com.kakarote.core.feign.oa.OaService;
import com.kakarote.core.feign.oa.entity.OaExamine;
import com.kakarote.core.feign.oa.entity.OaSearchBO;
import com.kakarote.core.servlet.ApplicationContextHolder;
import com.kakarote.core.utils.BiParamsUtil;
import com.kakarote.core.utils.ExcelParseUtil;
import com.kakarote.core.utils.UserCacheUtil;
import com.kakarote.core.utils.UserUtil;
import com.kakarote.examine.common.CommonDateTimeUtil;
import com.kakarote.examine.constant.ExamineCodeEnum;
import com.kakarote.examine.constant.ExamineStatusEnum;
import com.kakarote.examine.constant.ExamineTypeEnum;
import com.kakarote.examine.constant.SuperExamineConst;
import com.kakarote.examine.entity.BO.*;
import com.kakarote.examine.entity.PO.*;
import com.kakarote.examine.entity.VO.*;
import com.kakarote.examine.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class SuperExamineServiceImpl implements ISuperExamineService {

    @Autowired
    private OaService oaService;

    @Autowired
    private IExamineService examineService;

    @Autowired
    private IExamineGroupService examineGroupService;

    @Autowired
    private IExamineRecordService examineRecordService;

    @Autowired
    private ISuperExamineToDoService superExamineToDoService;

    @Autowired
    private IExamineRecordLogService examineRecordLogService;

    @Autowired
    private IExamineRecordLimitService recordLimitService;

    @Autowired
    private ExamineRecordServiceImpl examineRecordServiceImpl;

    @Autowired
    private ISuperExamineEmailService examineEmailService;

    @Autowired
    private IExamineFlowService examineFlowService;

    @Autowired
    private IExamineDelegateService examineDelegateService;

    @Autowired
    private AdminService adminService;

    @Override
    public List<ExamineVO> recentlyUsedExamineList() {
        return examineRecordService.recentlyUsedExamineList(UserUtil.isAdmin(), UserUtil.getUserId(), UserUtil.getUser().getDeptId());
    }


    /**
     * 首页-最近使用的申请
     *
     * @return
     */
    @Override
    public List<SupperExamineApplyVO> applyList() {
        List<SupperExamineApplyVO> supperExamineApplyVOList = new ArrayList<>();
        List<ExamineVO> recentlyUseExamineList = examineRecordService.recentlyUsedExamineList(UserUtil.isAdmin(), UserUtil.getUserId(), UserUtil.getUser().getDeptId());
        if (CollectionUtil.isNotEmpty(recentlyUseExamineList)) {
            recentlyUseExamineList.forEach(recentlyUseExamine -> {
                String[] icons = recentlyUseExamine.getExamineIcon().split(",");
                if (icons.length >= 1) {
                    recentlyUseExamine.setIconClass(icons[0]);
                }
                if (icons.length >= 2) {
                    recentlyUseExamine.setIconColor(icons[1]);
                }
            });
        }
        SupperExamineApplyVO recentlySupperExamineApplyVO = new SupperExamineApplyVO();
        recentlySupperExamineApplyVO.setGroupId(0L);
        recentlySupperExamineApplyVO.setGroupName("常用");
        recentlySupperExamineApplyVO.setSuperExamineVOList(recentlyUseExamineList);//添加常用列表

        supperExamineApplyVOList.add(recentlySupperExamineApplyVO);
        //todo 查询分组表
        List<ExamineGroup> groupList = examineGroupService.list();
        Long deptId = UserUtil.getUser().getDeptId();
        QueryParentDeptBO queryBO = new QueryParentDeptBO();
        queryBO.setDeptId(deptId);
        queryBO.setLevel(20);
        List<Long> ids = adminService.queryParentDeptIdByLevel(queryBO).getData();
        ids.add(deptId);
        List<Examine> examineList = examineService.queryLabelStatusWithSort(0, 1, UserUtil.isAdmin(), UserUtil.getUserId(), deptId, ids);
        if (CollectionUtil.isNotEmpty(examineList)) {
            List<ExamineVO> examineVOList = examineList.stream()
                    .map(examine -> {
                        ExamineVO examineVO = new ExamineVO();
                        BeanUtil.copyProperties(examine, examineVO);
                        if (ObjectUtil.isNotEmpty(examine.getExamineIcon())) {
                            String[] icons = examine.getExamineIcon().split(",");
                            if (icons.length >= 1) {
                                examineVO.setIconClass(icons[0]);
                            }
                            if (icons.length >= 2) {
                                examineVO.setIconColor(icons[1]);
                            }
                        }
                        return examineVO;
                    }).collect(Collectors.toList());
            Map<Long, List<ExamineVO>> examineListOfGroup = new HashMap<>();

            examineVOList.forEach(examineVO -> {
                Long groupId = examineVO.getGroupId();
                if (ObjectUtil.isNotNull(groupId)) {
                    List<ExamineVO> examineVOLisTmp = examineListOfGroup.get(groupId);
                    if (CollectionUtil.isEmpty(examineVOLisTmp)) {
                        examineVOLisTmp = new ArrayList<>();
                        examineListOfGroup.put(groupId, examineVOLisTmp);
                    }
                    examineVOLisTmp.add(examineVO);
                }
            });
            for (ExamineGroup groupInfo : groupList) {
                List<ExamineVO> examines = examineListOfGroup.get(groupInfo.getGroupId());
                if (CollectionUtil.isNotEmpty(examines)) {
                    SupperExamineApplyVO supperExamineApplyOfGroupVO = new SupperExamineApplyVO();
                    supperExamineApplyOfGroupVO.setGroupId(groupInfo.getGroupId());
                    supperExamineApplyOfGroupVO.setGroupName(groupInfo.getGroupName());
                    supperExamineApplyOfGroupVO.setSuperExamineVOList(examines);
                    supperExamineApplyVOList.add(supperExamineApplyOfGroupVO);
                }
            }
            //添加未分组数据

        }

        return supperExamineApplyVOList;
    }

    @Override
    public List<ExamineGroupVO> queryExamineGroup(ExaminePageBO examinePageBo) {
        List<ExamineVO> examineTreeVOList = new ArrayList<>();
        List<ExamineVO> emptyGroupExamineList = new ArrayList<>();
        Boolean isConnection = true;
        int five = 5;
        int six = 6;
        //客户管理系统流程
        if (Objects.equals(1, examinePageBo.getGroupType())) {
            examinePageBo.setLabelList(Arrays.asList(1, 2, 3));
        //hrm系统流程
        } else if (Objects.equals(five, examinePageBo.getGroupType())) {
            examinePageBo.setLabelList(Lists.newArrayList(4,91));
        //进销存系统
        } else if (Objects.equals(six, examinePageBo.getGroupType())) {
            examinePageBo.setLabelList(Arrays.asList(5, 6, 7, 8, 9, 10, 11, 12));
        //办公流程
        } else if (Objects.equals(0, examinePageBo.getGroupType())) {
            examinePageBo.setLabel(0);
        } else {
            isConnection = false;
        }
        if (isConnection) {
            examinePageBo.setIsPart(true);
            List<ExamineVO> examineVOList = examineService.queryAllList(examinePageBo);
            if (CollectionUtil.isNotEmpty(examineVOList)) {
                List<ExamineVO> examineTreeVOFilterList = examineVOList.stream().filter(examineVO -> ObjectUtil.isNotEmpty(examineVO.getGroupId())).collect(Collectors.toList());
                examineTreeVOList.addAll(examineTreeVOFilterList);
                emptyGroupExamineList = examineVOList.stream().filter(examineVO -> {
                    return ObjectUtil.isEmpty(examineVO.getGroupId());
                }).collect(Collectors.toList());
            }
        }
        Map<Long, List<ExamineVO>> examineGroupData = new HashMap<>();
        if (CollectionUtil.isNotEmpty(examineTreeVOList)) {
            Map<Long, List<ExamineVO>> groupDataMap = examineTreeVOList.stream().collect(Collectors.groupingBy(ExamineVO::getGroupId));
            examineGroupData.putAll(groupDataMap);
        }
        List<ExamineGroupVO> examineGroupVOListRet = new ArrayList<>();
        //添加未分组数据
        ExamineGroupVO defaultGroupVO = new ExamineGroupVO();
        defaultGroupVO.setExamineVOList(emptyGroupExamineList);

        List<ExamineGroup> examineGroupList = examineGroupService.lambdaQuery().eq(ObjectUtil.isNotNull(examinePageBo.getGroupType()), ExamineGroup::getGroupType, examinePageBo.getGroupType()).list();
        //获取默认分组
        ExamineGroup defaultGroup = null;
        if (CollectionUtil.isNotEmpty(examineGroupList)) {
            List<ExamineGroup> defaultGroups = examineGroupList.stream().filter(item -> (ObjectUtil.isNotEmpty(item.getGroupName()) && item.getGroupName().equals("默认"))).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(defaultGroups)) {
                defaultGroup = CollectionUtil.getFirst(defaultGroups);
            } else {
                defaultGroup = CollectionUtil.getFirst(examineGroupList);
            }
        } else {
            //新增默认分组
            defaultGroup = new ExamineGroup();
            defaultGroup.setGroupName("默认");
            defaultGroup.setGroupType(0);
            defaultGroup.setCreateUserId(UserUtil.getUserId());
            defaultGroup.setCreateTime(new Date());
            examineGroupService.save(defaultGroup);
            examineGroupList.add(defaultGroup);
        }
        defaultGroupVO.setGroupId(defaultGroup.getGroupId());
        defaultGroupVO.setGroupName(defaultGroup.getGroupName());
        //如果存在groupId为空,或者有groupId 但是不存在则新建默认分组
        if (CollectionUtil.isNotEmpty(emptyGroupExamineList)) {
            //设置未分组的流程groupId
            for (ExamineVO examineVO : emptyGroupExamineList) {
                examineService.lambdaUpdate().set(Examine::getGroupId, defaultGroup.getGroupId()).eq(Examine::getExamineId, examineVO.getExamineId()).update();
            }
        }
        if (CollectionUtil.isNotEmpty(examineGroupList)) {
            List<Long> groupIds = examineGroupList.stream().map(ExamineGroup::getGroupId).collect(Collectors.toList());
            //存在分组id但是无效的流程处理
            if (CollectionUtil.isNotEmpty(examineTreeVOList)) {
                List<ExamineVO> noValidGroupIds = examineTreeVOList.stream().filter(item -> !groupIds.contains(item.getGroupId())).collect(Collectors.toList());
                defaultGroupVO.getExamineVOList().addAll(noValidGroupIds);
                if (CollectionUtil.isNotEmpty(noValidGroupIds)) {
                    //设置无效分组的流程groupId
                    for (ExamineVO examineVO : noValidGroupIds) {
                        examineVO.setGroupId(defaultGroup.getGroupId());
                        examineService.lambdaUpdate().set(Examine::getGroupId, defaultGroup.getGroupId()).eq(Examine::getExamineId, examineVO.getExamineId()).update();
                    }
                }
            }
            List<ExamineGroupVO> examineGroupVOList = examineGroupList.stream().map(examineGroup -> {
                ExamineGroupVO examineGroupVO = new ExamineGroupVO();
                BeanUtil.copyProperties(examineGroup, examineGroupVO);
                List<ExamineVO> examineList = new ArrayList<>();
                List<ExamineVO> examineVOList = examineGroupData.get(examineGroup.getGroupId());
                if (examineGroup.getGroupId().equals(defaultGroupVO.getGroupId())) {
                    examineList.addAll(defaultGroupVO.getExamineVOList());
                }
                if (CollectionUtil.isNotEmpty(examineVOList)) {
                    examineList.addAll(examineVOList);
                }
                examineGroupVO.setExamineVOList(examineList);
                return examineGroupVO;
            }).collect(Collectors.toList());
            examineGroupVOListRet.addAll(examineGroupVOList);
        }
        return examineGroupVOListRet;
    }

    @Override
    public List<ExamineGroupVO> querySimplyExamineGroup(ExaminePageBO examinePageBo) {
        List<ExamineGroup> examineGroupList = examineGroupService.lambdaQuery().eq(ObjectUtil.isNotNull(examinePageBo.getGroupType()), ExamineGroup::getGroupType, examinePageBo.getGroupType()).list();
        if (CollectionUtil.isNotEmpty(examineGroupList)) {
            return examineGroupList.stream().map(examineGroup -> {
                ExamineGroupVO examineGroupVO = new ExamineGroupVO();
                BeanUtil.copyProperties(examineGroup, examineGroupVO);
                return examineGroupVO;
            }).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Override
    public BasePage<OaSuperExamineVO> querySuperExamineInfoByName(OaSearchBO oaSearchBO) {
        BasePage<OaSuperExamineVO> oaSuperExamineVOList = new BasePage<>();
        BasePage<OaExamine> oaExamineVOList = oaService.queryExamineByNameForPage(oaSearchBO).getData();
        BeanUtils.copyProperties(oaExamineVOList, oaSuperExamineVOList);
        if (CollectionUtil.isNotEmpty(oaExamineVOList.getList())) {
            List<OaSuperExamineVO> retList = new ArrayList<>();
            //获取wk_examine_record Id列表e
            oaExamineVOList.getList().forEach(oaExamineVO -> {
                OaSuperExamineVO oaSuperExamineVO = new OaSuperExamineVO();
                //获取预览审批总流程
                ExamineRecordVO examineRecordVO = examineRecordService.queryExamineRecordList(oaExamineVO.getExamineRecordId(), UserUtil.getUserId(), null, 1);
                ExamineFlowDataVO preExamineFlowDataVO = queryPreStepExamine(examineRecordVO.getExamineFlowList());
                oaSuperExamineVO.setPreHandleUserList(preExamineFlowDataVO.getUserList());
                ExamineRecordLog examineRecordLog = examineRecordLogService.lambdaQuery().eq(ExamineRecordLog::getRecordId, oaExamineVO.getExamineRecordId()).eq(ExamineRecordLog::getExamineStatus, ExamineStatusEnum.UNDERWAY.getStatus()).last("limit 1").one();
                if (ObjectUtil.isNull(examineRecordLog)) {
                    examineRecordLog = examineRecordLogService.lambdaQuery().eq(ExamineRecordLog::getRecordId, oaExamineVO.getExamineRecordId()).orderByDesc(ExamineRecordLog::getSort).last("limit 1").one();
                }
                if (ObjectUtil.isNotNull(examineRecordLog)) {
                    if (examineRecordLog.getExamineStatus().equals(ExamineStatusEnum.UNDERWAY.getStatus())) {//只有审核中的数据才有停留时间
                        LocalDateTime startTime = examineRecordLog.getCreateTime();
                        LocalDateTime end = LocalDateTime.now();
                        Duration duration = Duration.between(startTime, end);
                        long minutes = duration.toMinutes();//相差的分钟数
                        long hours = minutes / 60;
                        long minutesView = minutes % 60;
                        oaSuperExamineVO.setStayTime(hours + "小时" + minutesView + "分钟");
                    }
                }
                oaSuperExamineVO.setSubmitUser(examineRecordVO.getCreateUser());

                ExamineRecord examineRecord = examineRecordService.getById(oaExamineVO.getExamineRecordId());
                if (ObjectUtil.isNotEmpty(examineRecord)) {
                    Examine examineInfo = examineService.lambdaQuery().eq(Examine::getExamineId, examineRecord.getExamineId()).one();
                    oaSuperExamineVO.setExamineType(examineInfo.getExamineName());
                }
                oaSuperExamineVO.setOaExamineId(oaExamineVO.getExamineId());
                oaSuperExamineVO.setContent(oaExamineVO.getContent());
                oaSuperExamineVO.setSubmitDeptId(oaExamineVO.getSubmitDeptId());
                oaSuperExamineVO.setSubmitDeptName(oaExamineVO.getSubmitDeptName());
                retList.add(oaSuperExamineVO);
            });
            oaSuperExamineVOList.setList(retList);
        }
        return oaSuperExamineVOList;
    }


    /**
     * 获取前一步处理信息
     *
     * @param examineFlowList
     * @return
     */
    private ExamineFlowDataVO queryPreStepExamine(List<ExamineFlowDataVO> examineFlowList) {
        ExamineFlowDataVO preExamineFlowDataVO = new ExamineFlowDataVO();
        Boolean hasSet = false;
        if (CollectionUtil.isNotEmpty(examineFlowList)) {
            for (int i = 0; i < examineFlowList.size(); i++) {
                ExamineFlowDataVO examineFlowDataVO = examineFlowList.get(i);
                if (examineFlowDataVO.getExamineStatus().equals(ExamineStatusEnum.UNDERWAY.getStatus())) {
                    if (i >= 1) {
                        preExamineFlowDataVO = examineFlowList.get(i);
                        hasSet = true;
                    }
                }
            }
            if (!hasSet) {
                if (examineFlowList.size() >= 1) {
                    preExamineFlowDataVO = examineFlowList.get(examineFlowList.size() - 1);
                }
            }
        }
        return preExamineFlowDataVO;
    }

    @Override
    public List<ExamineVO> querySuperExamineByName(SuperExamineSearchBO superExamineSearchBO) {
        List<Examine> examineList = examineService.lambdaQuery().like(Examine::getExamineName, superExamineSearchBO.getExamineName()).eq(Examine::getLabel, 0).eq(ObjectUtil.isNotNull(superExamineSearchBO.getGroupId()), Examine::getGroupId, superExamineSearchBO.getGroupId()).ne(Examine::getStatus, SuperExamineConst.STATUS_DEL).list();
        if (CollectionUtil.isNotEmpty(examineList)) {
            List<ExamineVO> examineVOList = examineList.stream().map(examine -> {
                ExamineVO examineVO = new ExamineVO();
                BeanUtil.copyProperties(examine, examineVO);
                if (ObjectUtil.isNotEmpty(examine.getExamineIcon())) {
                    String[] icons = examine.getExamineIcon().split(",");
                    if (icons.length >= 1) {
                        examineVO.setIconClass(icons[0]);
                    }
                    if (icons.length >= 2) {
                        examineVO.setIconColor(icons[1]);
                    }
                }
                return examineVO;
            }).collect(Collectors.toList());
            return examineVOList;
        }
        return new ArrayList<>();
    }

    @Override
    public SuperExamineBackLogVO queryBackLogInfo() {
        //todo 这里查询待办审批数据id
        SuperExamineBackLogVO superExamineBackLogVO = new SuperExamineBackLogVO();
        ExaminePageBO examinePageBo = new ExaminePageBO();
        examinePageBo.setStatus(1);
        BasePage<com.kakarote.core.feign.oa.entity.ExamineVO> examineVOBasePage = examineService.queryOaExamineList(examinePageBo);
        if (CollectionUtil.isNotEmpty(examineVOBasePage.getList())) {
            List<SuperExamineBackLogVO.SuperExamineBackLogInfo> superExamineBackLogInfoList = new ArrayList<>();
            for (com.kakarote.core.feign.oa.entity.ExamineVO examineVO : examineVOBasePage.getList()) {
                SuperExamineBackLogVO.SuperExamineBackLogInfo superExamineBackLogInfo = new SuperExamineBackLogVO.SuperExamineBackLogInfo();
                ExamineRecord examineRecord = examineRecordService.getById(examineVO.getExamineRecordId());
                if (BeanUtil.isNotEmpty(examineRecord) && ObjectUtil.isNotEmpty(examineRecord.getRecordId())) {
                    //获取正在审批的这一层用于计算停留时间
                    ExamineRecordLog examineRecordLog = examineRecordLogService.lambdaQuery().eq(ExamineRecordLog::getRecordId, examineRecord.getRecordId()).eq(ExamineRecordLog::getExamineStatus, ExamineStatusEnum.UNDERWAY.getStatus()).last("limit 1").one();
                    LocalDateTime startTime = examineRecordLog.getCreateTime();
                    superExamineBackLogInfo.setStayTime(CommonDateTimeUtil.examineStopTimeStr(startTime, LocalDateTime.now()));
                }

                superExamineBackLogInfo.setExamineId(examineVO.getExamineId());
                superExamineBackLogInfo.setContent(examineVO.getContent());
                superExamineBackLogInfoList.add(superExamineBackLogInfo);
            }
            superExamineBackLogVO.setNum(examineVOBasePage.getTotal());//todo 这里也要进行修改
            superExamineBackLogVO.setSuperExamineBackLogInfoList(superExamineBackLogInfoList);
        }
        return superExamineBackLogVO;
    }


    @Override
    public SuperExamineTraceVO queryTraceInfo() {
        SuperExamineTraceVO superExamineTraceVO = new SuperExamineTraceVO();
        SuperExamineTraceVO.SuperExamineTraceInfoVO superExamineTraceInfoVO = new SuperExamineTraceVO.SuperExamineTraceInfoVO();
        //        //查询由当前用户创建的最新的正在处理中的审核流程-OA类型
        List<ExamineRecord> list = examineRecordService.lambdaQuery().eq(ExamineRecord::getCreateUserId, UserUtil.getUserId()).eq(ExamineRecord::getLabel, 0).eq(ExamineRecord::getExamineStatus, ExamineStatusEnum.UNDERWAY.getStatus()).orderByDesc(ExamineRecord::getCreateTime).list();
        if (CollectionUtil.isNotEmpty(list)) {
            ExamineRecord examineRecord = list.get(0);
            SuperExamineRecordVO superExamineRecordVO = superExamineToDoService.queryExamineRecordList(examineRecord.getRecordId(), UserUtil.getUserId(), null, 1);
            List<SuperExamineFlowDataVO> examineFlowList = superExamineRecordVO.getExamineFlowList();
            List<Map<String, Object>> curStepHandleUserList = new ArrayList<>();
            //0917 需求变动
            //1、审批通过的节点只显示节点审批人,绿色对号显示
            //2、审批中/待审批如果不确定审批人（会签和或签时），则显示待审批节点的所有人
            //3、如果是依次审批，则正常显示对应人员（假如依次审批的节点有三个人：张三、李四、王五。且张三审核通过。则在跟踪中的三个节点则依次是张三、李四、王五）
            Boolean flag = Boolean.FALSE;
            int examineFlowSize = examineFlowList.size();
            for (int i = 0; i < examineFlowSize; i++) {
                SuperExamineFlowDataVO item = examineFlowList.get(i);
                if (ExamineStatusEnum.UNDERWAY.getStatus().equals(item.getExamineStatus())) {
                    flag = Boolean.TRUE;
                    if (ObjectUtil.isNotEmpty(item.getType()) && item.getType().equals(1)) {
                        ////判定是依次审批
                        curStepHandleUserList = subRetStepHandleUserTurn(i > 0 ? examineFlowList.get(i - 1) : null, item, i < examineFlowSize - 1 ? examineFlowList.get(i + 1) : null);
                    } else {
                        //前
                        if (i > 0) {
                            curStepHandleUserList.add(subRetStepHandleUser(examineFlowList.get(i - 1)));
                        }
                        //中
                        curStepHandleUserList.add(subRetStepHandleUser(item));
                        //后
                        if (i < examineFlowSize - 1) {
                            curStepHandleUserList.add(subRetStepHandleUser(examineFlowList.get(i + 1)));
                        }
                    }
                    break;
                }
            }
            if (!flag) {
                if (examineFlowSize > 3) {
                    curStepHandleUserList.add(subRetStepHandleUser(examineFlowList.get(examineFlowSize - 3)));
                    curStepHandleUserList.add(subRetStepHandleUser(examineFlowList.get(examineFlowSize - 2)));
                    curStepHandleUserList.add(subRetStepHandleUser(examineFlowList.get(examineFlowSize - 1)));
                } else {
                    for (SuperExamineFlowDataVO superExamineFlowDataVO : examineFlowList) {
                        curStepHandleUserList.add(subRetStepHandleUser(superExamineFlowDataVO));
                    }
                }
            }
            superExamineTraceInfoVO.setAllStepHandleUserList(curStepHandleUserList);
            com.kakarote.core.feign.oa.entity.ExamineVO examineVO = oaService.getOaExamineById(examineRecord.getTypeId()).getData();
            if (ObjectUtil.isNotEmpty(examineVO)) {
                superExamineTraceInfoVO.setOaExamineId(examineVO.getExamineId());
                superExamineTraceInfoVO.setOaCategoryId(examineVO.getCategoryId());
                superExamineTraceInfoVO.setExamineRecordId(examineVO.getExamineRecordId());
                superExamineTraceInfoVO.setContent(examineVO.getContent());
            }
            superExamineTraceVO.setSuperExamineTraceInfoVO(superExamineTraceInfoVO);
            superExamineTraceVO.setNum(list.size());

        }
        return superExamineTraceVO;
    }

    private List<Map<String, Object>> subRetStepHandleUserTurn(SuperExamineFlowDataVO itempre, SuperExamineFlowDataVO item, SuperExamineFlowDataVO itemafter) {
        List<Map<String, Object>> ret = new ArrayList<>();
        List<Map<String, Object>> handlerUserList = item.getUserList();
        for (int j = 0; j < handlerUserList.size(); j++) {
            Integer examineStatus = (Integer) handlerUserList.get(j).get("examineStatus");
            if (ExamineStatusEnum.UNDERWAY.getStatus().equals(examineStatus)) {
                //前
                if (j > 0) {
                    Map<String, Object> curStepInfo1 = new HashMap<>();
                    Map<String, Object> curStepUser1 = new HashMap<>();
                    String stageName1 = String.valueOf(handlerUserList.get(j - 1).get("realname"));
                    String outerUserEmail1 = String.valueOf(handlerUserList.get(j - 1).get("outerUserEmail"));
                    String examineTime1 = String.valueOf(handlerUserList.get(j - 1).get("examineTime"));
                    String createTime1 = String.valueOf(handlerUserList.get(j - 1).get("createTime"));
                    curStepUser1.put("stageName", ObjectUtil.isNotEmpty(handlerUserList.get(j - 1).get("realname")) ? stageName1 : outerUserEmail1);
                    curStepUser1.put("time", examineTime1);
                    LocalDateTime parsePayTime1 = LocalDateTimeUtil.parse(createTime1);
                    String stayTime1 = calculationStayTime(parsePayTime1);
                    curStepUser1.put("stayTime", stayTime1);
                    curStepInfo1.put("userList", Arrays.asList(curStepUser1));
                    curStepInfo1.put("type", item.getType());
                    ret.add(curStepInfo1);
                } else {
                    if (ObjectUtil.isNotEmpty(itempre)) {
                        ret.add(subRetStepHandleUser(itempre));
                    }
                }
                //中
                Map<String, Object> curStepInfo = new HashMap<>();
                Map<String, Object> curStepUser = new HashMap<>();
                String stageName = String.valueOf(handlerUserList.get(j).get("realname"));
                String outerUserEmail = String.valueOf(handlerUserList.get(j).get("outerUserEmail"));
                String examineTime = String.valueOf(handlerUserList.get(j).get("examineTime"));
                String createTime = String.valueOf(handlerUserList.get(j).get("createTime"));
                LocalDateTime parsePayTime = LocalDateTimeUtil.parse(createTime);
                String stayTime = calculationStayTime(parsePayTime);
                curStepUser.put("stayTime", stayTime);
                curStepUser.put("stageName", ObjectUtil.isNotEmpty(handlerUserList.get(j).get("realname")) ? stageName : outerUserEmail);
                curStepUser.put("time", examineTime);
                curStepInfo.put("userList", Arrays.asList(curStepUser));
                curStepInfo.put("type", item.getType());
                ret.add(curStepInfo);
                //后
                if (j < handlerUserList.size() - 1) {
                    Map<String, Object> curStepInfo2 = new HashMap<>();
                    Map<String, Object> curStepUser2 = new HashMap<>();
                    String stageName2 = String.valueOf(handlerUserList.get(j + 1).get("realname"));
                    String outerUserEmail2 = String.valueOf(handlerUserList.get(j + 1).get("outerUserEmail"));
                    String examineTime2 = String.valueOf(handlerUserList.get(j + 1).get("examineTime"));
                    String createTime2 = String.valueOf(handlerUserList.get(j + 1).get("createTime"));
                    curStepUser2.put("stageName", ObjectUtil.isNotEmpty(handlerUserList.get(j + 1).get("realname")) ? stageName2 : outerUserEmail2);
                    curStepUser2.put("time", examineTime2);
                    LocalDateTime parsePayTime2 = LocalDateTimeUtil.parse(createTime2);
                    String stayTime2 = calculationStayTime(parsePayTime2);
                    curStepUser2.put("stayTime", stayTime2);
                    curStepInfo2.put("userList", Arrays.asList(curStepUser2));
                    curStepInfo2.put("type", item.getType());
                    ret.add(curStepInfo2);
                } else {
                    if (ObjectUtil.isNotEmpty(itemafter)) {
                        ret.add(subRetStepHandleUser(itemafter));
                    }
                }
                break;
            }
        }
        //后

        return ret;
    }

    private Map<String, Object> subRetStepHandleUser(SuperExamineFlowDataVO item) {
        Map<String, Object> curStepInfo = new HashMap<>();
        if (ObjectUtil.isEmpty(item)) {
            return curStepInfo;
        }
        List<Map<String, Object>> handlerUserList = item.getUserList();
        List<Map<String, Object>> curStepHandlerUserList = new ArrayList<>();
        for (Map<String, Object> handlerUserInfo : handlerUserList) {
            Map<String, Object> curStepUser = new HashMap<>();
            String stageName = String.valueOf(handlerUserInfo.get("realname"));
            String outerUserEmail = String.valueOf(handlerUserInfo.get("outerUserEmail"));
            String examineTime = String.valueOf(handlerUserInfo.get("examineTime"));
            String createTime = String.valueOf(handlerUserInfo.get("createTime"));
            curStepUser.put("stageName", ObjectUtil.isNotEmpty(handlerUserInfo.get("realname")) ? stageName : outerUserEmail);
            curStepUser.put("time", examineTime);
            Integer examineStatus = (Integer) handlerUserInfo.get("examineStatus");
            if (ExamineStatusEnum.UNDERWAY.getStatus().equals(examineStatus)) {
                LocalDateTime parsePayTime = LocalDateTimeUtil.parse(createTime);
                String stayTime = calculationStayTime(parsePayTime);
                curStepUser.put("stayTime", stayTime);
            }
            curStepHandlerUserList.add(curStepUser);

        }
        curStepInfo.put("userList", curStepHandlerUserList);
        curStepInfo.put("type", item.getType());
        return curStepInfo;
    }


    //计算停留时间
    private String calculationStayTime(LocalDateTime time) {
        LocalDateTime end = LocalDateTime.now();
        Duration duration = Duration.between(time, end);
        String minutesStr = "00";
        String hoursStr = "00";
        long minutes = duration.toMinutes();//相差的分钟数
        long hour = minutes / 60;
        if (hour > 0) {
            if (hour < 10) {
                hoursStr = "0" + hour;
            } else {
                hoursStr = String.valueOf(hour);
            }
        }

        long minute = minutes % 60;
        if (minute > 0) {
            if (minute < 10) {
                minutesStr = "0" + minute;
            } else {
                minutesStr = String.valueOf(minute);
            }
        }
        return hoursStr + "小时" + minutesStr + "分钟";
    }


    @Override
    public OperationLog addOrUpdateExamineGroup(ExamineGroupBO examineGroupBO) {
        OperationLog operationLog = new OperationLog();
        if (ObjectUtil.isNotNull(examineGroupBO.getGroupId())) {
            ExamineGroup examineGroup = examineGroupService.lambdaQuery().eq(ExamineGroup::getGroupId, examineGroupBO.getGroupId()).one();
            if (ObjectUtil.isNull(examineGroup)) {
                throw new CrmException(ExamineCodeEnum.EXAMINE_GROUP_IS_NULL_ERROR);
            }
            operationLog.setOperationObject(examineGroupBO.getGroupName());
            operationLog.setOperationInfo(StrUtil.format("审批分组名称由{}改为{}", examineGroup.getGroupName(), examineGroupBO.getGroupName()));
            operationLog.setBehavior(BehaviorEnum.UPDATE);
            examineGroup.setGroupName(examineGroupBO.getGroupName());//目前仅支持修改分组名称
            Long deptId = UserUtil.getUser().getDeptId();
            examineGroup.setDeptId(deptId);
            examineGroupService.updateById(examineGroup);
        } else {
            ExamineGroup examineGroup = new ExamineGroup();
            //0921 校验分组名称不重复
            List<ExamineGroup> list = examineGroupService.lambdaQuery().eq(ExamineGroup::getGroupName, examineGroupBO.getGroupName()).list();
            if (CollectionUtil.isNotEmpty(list)) {
                throw new CrmException(ExamineCodeEnum.EXAMINE_GROUP_NAME_IS_USE_ERROR);
            }
            //---------------------------------
            BeanUtil.copyProperties(examineGroupBO, examineGroup);
            examineGroup.setGroupType(0);//目前默认都是新建的OA类型分组
            examineGroup.setCreateTime(new Date());
            examineGroupService.save(examineGroup);

            operationLog.setBehavior(BehaviorEnum.SAVE);
            operationLog.setOperationObject(examineGroup.getGroupName());
            operationLog.setOperationInfo("新建审批分组：" + examineGroup.getGroupName());
        }

        return operationLog;
    }


    @Override
    public List<OperationLog> delExamineGroup(List<Long> groupIdList) {
        List<OperationLog> operationLogList = new ArrayList<>();
        groupIdList.forEach(groupId -> {
            boolean exists = examineService.lambdaQuery().eq(Examine::getGroupId, groupId).ne(Examine::getStatus, 3).exists();
            if (!exists) {
                OperationLog operationLog = new OperationLog();
                ExamineGroup examineGroup = examineGroupService.getById(groupId);
                operationLog.setOperationObject(examineGroup.getGroupName());
                operationLog.setOperationInfo("删除审批分组：" + examineGroup.getGroupName());
                operationLogList.add(operationLog);
                examineGroupService.lambdaUpdate().in(ExamineGroup::getGroupId, groupIdList).remove();
            } else {
                throw new CrmException(ExamineCodeEnum.EXAMINE_GROUP_IS_USE);
            }
        });
        return operationLogList;
    }

    @Override
    public List<Long> queryTimeOutRecordList() {
        return recordLimitService.queryRecordList();
    }




    @Override
    public SuperExamineReportHeadVO getExamineReportInfo(SuperExamineReportSearchBO searchBO) {
        BiParamsUtil.BiTimeEntity biTimeEntity = new BiParamsUtil.BiTimeEntity();
        BiParamsUtil.analyzeDate(biTimeEntity, searchBO);
        searchBO.setStartTime(biTimeEntity.getBeginDate());
        searchBO.setEndTime(biTimeEntity.getEndDate());
        Set<Long> allUserList = BiParamsUtil.getAllUserList(searchBO);
        if (CollectionUtil.isNotEmpty(allUserList)) {
            searchBO.setUserIdList(new ArrayList<>(allUserList));
        }
        return examineRecordService.getExamineReportInfo(searchBO);
    }

    @Override
    public BasePage<SuperExamineReportListVO> getExamineReportList(SuperExamineReportSearchBO searchBO) {

        BiParamsUtil.BiTimeEntity biTimeEntity = new BiParamsUtil.BiTimeEntity();
        BiParamsUtil.analyzeDate(biTimeEntity, searchBO);
        searchBO.setStartTime(biTimeEntity.getBeginDate());
        searchBO.setEndTime(biTimeEntity.getEndDate());
        Set<Long> allUserList = BiParamsUtil.getAllUserList(searchBO);
        if (CollectionUtil.isNotEmpty(allUserList)) {
            searchBO.setUserIdList(new ArrayList<>(allUserList));
        }
        return examineRecordService.getSuperExamineReportList(searchBO);
    }

    @Override
    public void getExamineReporExport(HttpServletResponse response, SuperExamineReportSearchBO searchBO) {
        searchBO.setPageType(0);
        BasePage<SuperExamineReportListVO> retList = getExamineReportList(searchBO);
        List<Map<String, Object>> dataList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(retList.getList())) {
            dataList = retList.getList().stream().map(BeanUtil::beanToMap).collect(Collectors.toList());
        }
        // 表格的标题list
        List<ExcelParseUtil.ExcelDataEntity> titleList = Arrays.asList(new ExcelParseUtil.ExcelDataEntity("name", "姓名", FieldEnum.TEXT.getType()), new ExcelParseUtil.ExcelDataEntity("deptName", "所属部门", FieldEnum.TEXT.getType()), new ExcelParseUtil.ExcelDataEntity("examineApplyNum", "提交数", FieldEnum.TEXT.getType()), new ExcelParseUtil.ExcelDataEntity("examineNum", "审批总次数", FieldEnum.TEXT.getType()), new ExcelParseUtil.ExcelDataEntity("examineTimeAvgNum", "平均时效（小时）", FieldEnum.TEXT.getType()), new ExcelParseUtil.ExcelDataEntity("examineAvgNum5", "5小时内完成审批次数", FieldEnum.TEXT.getType()), new ExcelParseUtil.ExcelDataEntity("examineFiveAvgPercent", "5小时内完成审批次数占比", FieldEnum.TEXT.getType()), new ExcelParseUtil.ExcelDataEntity("examineAvgNum512", "5-12小时内完成审批次数", FieldEnum.TEXT.getType()), new ExcelParseUtil.ExcelDataEntity("examineFiveAvgPercent512", "5-12小时内完成审批次数占比", FieldEnum.TEXT.getType()), new ExcelParseUtil.ExcelDataEntity("examineAvgNum1224", "12-24小时内完成审批次数", FieldEnum.TEXT.getType()), new ExcelParseUtil.ExcelDataEntity("examineFiveAvgPercent1224", "12-24小时内完成审批次数占比", FieldEnum.TEXT.getType()), new ExcelParseUtil.ExcelDataEntity("examineAvgNum24", "超24小时完成审批次数", FieldEnum.TEXT.getType()), new ExcelParseUtil.ExcelDataEntity("examineFiveAvgPercent24", "超24小时完成审批次数占比", FieldEnum.TEXT.getType()), new ExcelParseUtil.ExcelDataEntity("examineLongTime", "最长审批用时（小时）", FieldEnum.TEXT.getType()), new ExcelParseUtil.ExcelDataEntity("examineTypeIdByYear", "当年累计文件量", FieldEnum.TEXT.getType()), new ExcelParseUtil.ExcelDataEntity("examineTimeAvgByYear", "当年平均审批时效", FieldEnum.TEXT.getType()));
        ExcelParseUtil.exportExcel(dataList, new ExcelParseUtil.ExcelParseService() {
            @Override
            public String getExcelName() {
                return "个人审批导出";
            }
        }, titleList, response, searchBO.getIsXls());
    }


    @Override
    public BasePage<com.kakarote.core.feign.oa.entity.ExamineVO> getExamineReportSubList(SuperExamineReportSubSearchBO searchBO) {
        BiParamsUtil.BiTimeEntity biTimeEntity = new BiParamsUtil.BiTimeEntity();
        BiParamsUtil.analyzeDate(biTimeEntity, searchBO);
        searchBO.setStartTime(biTimeEntity.getBeginDate());
        searchBO.setEndTime(biTimeEntity.getEndDate());
        examineRecordService.getSuperExamineReportList(searchBO);
        BasePage<ExamineRecord> basePage = examineRecordService.getExamineReportSubList(searchBO);
        BasePage<com.kakarote.core.feign.oa.entity.ExamineVO> page = new BasePage<>(basePage.getCurrent(), basePage.getSize(), basePage.getTotal());
        List<com.kakarote.core.feign.oa.entity.ExamineVO> examineVoList = new ArrayList<>();
        for (ExamineRecord examineRecord : basePage.getList()) {
            Long typeId = examineRecord.getTypeId();
            if (typeId == null) {
                continue;
            }
            com.kakarote.core.feign.oa.entity.ExamineVO examineVO = oaService.getOaExamineById(typeId).getData();
            examineVO.setCreateTime(examineRecord.getCreateTime());
            //---------------------------------------
            SimpleUser lastExamineUser = new SimpleUser();
            List<ExamineRecordLog> hadRecordLogList = examineRecordLogService.lambdaQuery().eq(ExamineRecordLog::getRecordId, examineRecord.getRecordId()).ne(ExamineRecordLog::getExamineStatus, 3).orderByDesc(ExamineRecordLog::getCreateTime).list();
            if (CollectionUtil.isNotEmpty(hadRecordLogList)) {
                ExamineRecordLog examineRecordLog = hadRecordLogList.get(0);
                if (ObjectUtil.isNotEmpty(examineRecordLog.getExamineUserEmail())) {
                    lastExamineUser.setRealname(examineRecordLog.getExamineUserEmail());
                } else if (ObjectUtil.isNotEmpty(examineRecordLog.getType())) {
                    lastExamineUser = UserCacheUtil.getSimpleUser(examineRecordLog.getExamineUserId());
                } else {
                    lastExamineUser = UserCacheUtil.getSimpleUser(examineRecordLog.getCreateUserId());
                }
            }
            //上一步处理人
            examineVO.setLastHandleUser(lastExamineUser);
            //审批停留时间
            List<ExamineRecordLog> havingRecordLogList = examineRecordLogService.lambdaQuery().eq(ExamineRecordLog::getRecordId, examineRecord.getRecordId()).eq(ExamineRecordLog::getExamineStatus, 3).orderByDesc(ExamineRecordLog::getCreateTime).list();
            if (CollectionUtil.isNotEmpty(havingRecordLogList)) {
                StringBuilder currentUserStr = new StringBuilder();
                for (ExamineRecordLog examineRecordLog : havingRecordLogList) {
                    if (ObjectUtil.isNotEmpty(examineRecordLog.getExamineUserEmail())) {
                        currentUserStr.append(examineRecordLog.getExamineUserEmail()).append(";");
                    } else {
                        currentUserStr.append(UserCacheUtil.getUserName(examineRecordLog.getExamineUserId())).append(";");
                    }
                }
                if (havingRecordLogList.size() == 1) {
                    currentUserStr = new StringBuilder(currentUserStr.toString().replace(";", ""));
                }
                examineVO.setCurrentExamineUser(currentUserStr.toString());
                examineVO.setCurrentStopTime(CommonDateTimeUtil.examineStopTimeStr(havingRecordLogList.get(0).getCreateTime(), LocalDateTime.now()));
            }
            //---------------------------------------
            examineVoList.add(examineVO);
        }
        page.setList(examineVoList);
        return page;
    }

    /**
     * 功能描述: <br>
     * 〈无分组申请列表〉
     *
     * @param:
     * @throws:
     * @author: zyh
     * @date: 2022/10/18 14:33
     */
    @Override
    public List<ExamineVO> uniApplyList() {
        Long deptId = UserUtil.getUser().getDeptId();
        QueryParentDeptBO queryBO = new QueryParentDeptBO();
        queryBO.setDeptId(deptId);
        queryBO.setLevel(20);
        List<Long> ids = adminService.queryParentDeptIdByLevel(queryBO).getData();
        ids.add(deptId);
        List<Examine> examineList = examineService.queryLabelStatusWithSort(0, 1, UserUtil.isAdmin(), UserUtil.getUserId(), deptId, ids);
        if (CollectionUtil.isNotEmpty(examineList)) {
            examineList.stream().map(examine -> {
                ExamineVO examineVO = new ExamineVO();
                BeanUtil.copyProperties(examine, examineVO);
                if (ObjectUtil.isNotEmpty(examine.getExamineIcon())) {
                    String[] icons = examine.getExamineIcon().split(",");
                    if (icons.length >= 1) {
                        examineVO.setIconClass(icons[0]);
                    }
                    if (icons.length >= 2) {
                        examineVO.setIconColor(icons[1]);
                    }
                }
                return examineVO;
            }).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    /**
     * 功能描述: <br>
     * 〈设置邮件审批有效性〉
     *
     * @param: examineBO
     * @return: void
     * @throws:
     * @author: zyh
     * @date: 2022/10/29 17:14
     */
    @Override
    public void setEmailAuditValid(ExamineEmailBO examineBO) {
        //查询当前审批业务流程是否已经发送邮件
        List<ExamineRecordEmail> list = selectEmailToken(examineBO);
        if (CollectionUtil.isEmpty(list)) {
            //先删除相同人员的历史无效信息，清理数据
            deleteEmailToken(examineBO);
            //增加新的token
            adddEmailToken(examineBO);
        }

    }

    /**
     * 获取流程邮件人员token有效性
     */
    @Override
    public ExamineRecordEmail getEmailToken(String key) {
        if (StrUtil.isBlank(key)) {
            return new ExamineRecordEmail();
        }
        LambdaQueryWrapper<ExamineRecordEmail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamineRecordEmail::getEmailToken, key);
        queryWrapper.ge(ExamineRecordEmail::getValidEndTime, LocalDateTime.now());
        queryWrapper.orderByDesc(ExamineRecordEmail::getCreateTime);
        List<ExamineRecordEmail> list = examineEmailService.list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            return new ExamineRecordEmail();
        }
        return CollectionUtil.getFirst(list);
    }

    /**
     * 获取流程邮件人员token有效性
     */
    public List<ExamineRecordEmail> selectEmailToken(ExamineEmailBO examineBO) {
        LambdaQueryWrapper<ExamineRecordEmail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ExamineRecordEmail::getRecordId, examineBO.getRecordId());
        queryWrapper.eq(ExamineRecordEmail::getFlowId, examineBO.getFlowId());
        queryWrapper.eq(ExamineRecordEmail::getToEmail, examineBO.getToEmail());
        queryWrapper.ge(ExamineRecordEmail::getValidEndTime, LocalDateTime.now());
        return examineEmailService.list(queryWrapper);
    }

    /**
     * 删除流程邮件人员token有效性
     */
    public void deleteEmailToken(ExamineEmailBO examineBO) {
        LambdaUpdateWrapper<ExamineRecordEmail> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ExamineRecordEmail::getRecordId, examineBO.getRecordId());
        updateWrapper.eq(ExamineRecordEmail::getFlowId, examineBO.getFlowId());
        updateWrapper.eq(ExamineRecordEmail::getToEmail, examineBO.getToEmail());
        updateWrapper.le(ExamineRecordEmail::getValidEndTime, LocalDateTime.now());
        examineEmailService.remove(updateWrapper);
    }

    /**
     * 添加流程邮件人员token有效性
     */
    public void adddEmailToken(ExamineEmailBO examineBO) {
        ExamineRecordEmail examineRecordEmail = new ExamineRecordEmail();
        BeanUtil.copyProperties(examineBO, examineRecordEmail);
        examineRecordEmail.setValidEndTime(LocalDateTime.now().plusDays(1L));
        examineEmailService.save(examineRecordEmail);
    }


    /**
     * 设置节点自选
     *
     * @param flowFinalBO
     */
    @Override
    public void setNodeOptional(ExamineFlowFinalBO flowFinalBO) {

        if (ObjectUtil.isEmpty(flowFinalBO.getUserList())) {
            throw new CrmException(SystemCodeEnum.SYSTEM_NO_VALID);
        }

        ExamineTypeService examineTypeService = ApplicationContextHolder.getBean(ExamineTypeEnum.valueOf(flowFinalBO.getExamineType()).getServerName());
        ExamineFlow examineFlow = examineFlowService.getById(flowFinalBO.getFlowId());
        ExamineRecord examineRecord = examineRecordServiceImpl.getById(flowFinalBO.getRecordId());
        synchronized (this) {
            examineTypeService.disposeFlow(examineFlow, flowFinalBO.getRecordId(), flowFinalBO.getUserList(), null, false);
            //处理流程委托
            examineDelegateService.disposeDelegate(examineFlow.getFlowId(), flowFinalBO.getRecordId(), examineRecord.getLabel());
            examineTypeService.handleRecordLogByAdvancedConfig(examineRecord.getRecordId(), examineFlow.getFlowId(), false, null);

        }
        // 发送通知
        List<ExamineRecordLog> recordLogs = examineRecordLogService.lambdaQuery().eq(ExamineRecordLog::getRecordId, flowFinalBO.getRecordId()).eq(ExamineRecordLog::getFlowId, examineFlow.getFlowId()).eq(ExamineRecordLog::getExamineStatus, ExamineStatusEnum.UNDERWAY.getStatus()).list();

        for (ExamineRecordLog recordLog : recordLogs) {
            if (ObjectUtil.isNotEmpty(recordLog.getExamineUserEmail())) {
            } else {
                examineRecordService.addMessage(examineService.getById(examineRecord.getExamineId()), recordLog, examineRecord.getCreateUserId(), examineRecord.getExamineReason(), null);
            }
        }

    }
}
